package com.goodving.guli.model.weightsyncupload;

import android.content.Context;

import com.goodving.guli.bt.model.LoginData;
import com.goodving.guli.bt.model.SportBaseInfo;
import com.goodving.guli.bt.model.WeightInfo;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.model.uploadhelper.C1312a;
import com.goodving.guli.model.uploadhelper.C1315c;
import com.goodving.guli.model.uploadhelper.SyncerUpload;
import com.goodving.guli.model.uploadhelper.p074a.C1304h;
import com.goodving.guli.model.uploadhelper.p074a.C1308d;
import com.goodving.guli.model.uploadhelper.p074a.C1309e;
import com.goodving.guli.model.uploadhelper.p074a.C1310f;
import com.goodving.guli.model.uploadhelper.p074a.C1311i;
import com.goodving.guli.model.uploadhelper.p074a.p075a.C1301a;
import com.goodving.guli.model.uploadhelper.p074a.p075a.ResponseHandler;
import com.goodving.guli.model.uploadhelper.p076b.C1313a;
import com.goodving.guli.server.ClientDataConstants;
import com.goodving.guli.server.ClientConstant;
import com.goodving.guli.server.URL;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/* renamed from: cn.com.smartdevices.bracelet.weight.a.f */
public class SyncerWeightDownload extends SyncerUpload<C2001i> { // C1998f
    private static final String f11283a = "Syncer.Weight.Download";
    private Context f11284b;
    private LoginData f11285c;


    public SyncerWeightDownload(Context context, LoginData loginData) {
        this(context, loginData, new C2001i());
    }

    public SyncerWeightDownload(Context context, LoginData loginData, C2001i c2001i) {
        super(c2001i);
        this.f11284b = context;
        this.f11285c = loginData;
    }

    public String tag() {
        return "Syncer.Weight.Download_@" + Integer.toHexString(hashCode());
    }

    class C2003k extends C1309e<List<WeightInfo>> {
        private C2003k() {
        }

        public void m11511a(List<WeightInfo> list) {
            for (WeightInfo weightInfo : list) {
                weightInfo.synced = 1;
            }
            success(WeightInfoManager.getInstance().m15756a((List) list));
        }
    }

    class C2000h extends C1310f {
        final /* synthetic */ SyncerWeightDownload f11287a;

        C2000h(SyncerWeightDownload c1998f) {
            this.f11287a = c1998f;
        }

        public void then(C1315c c1315c) {
            if (c1315c.m11538h()) {
                this.f11287a.success();
                GuliLog.tracePrintInfo(this.f11287a.tag(), "Keep Weights Sync To Local State : Successed!!");
                Keeper.keepScaleSyncedWeightInfosFromServer(true);
            }
        }
    }

    class C1999g extends C1311i<C2001i, RequestParams> {
        final /* synthetic */ SyncerWeightDownload f11286a;

        C1999g(SyncerWeightDownload c1998f) {
            this.f11286a = c1998f;
        }

        public RequestParams m15673a(C2001i c2001i) {
            RequestParams a = ClientDataConstants.m11558a(this.f11286a.f11285c);
            a.put("fuid", c2001i.f11290c);
            if (c2001i.f11288a > -1) {
                a.put(SportBaseInfo.KEY_START_TIME, c2001i.f11288a);
            }
            if (c2001i.f11289b > -1) {
                a.put(SportBaseInfo.KEY_END_TIME, c2001i.f11289b);
            }
            if (c2001i.f11291d) {
                a.put("fuid", "all");
            } else {
                a.put("fuid", c2001i.f11290c);
            }
            a.put(ClientDataConstants.aa, 1);
            return a;
        }

        public /* synthetic */ RequestParams wrap(C2001i obj) {
            return m15673a((C2001i) obj);
        }
    }

    class C2002j extends C1308d<String, List<WeightInfo>> {
        private C2002j() {
        }

        public List<WeightInfo> m11510a(String str) {
            List<WeightInfo> arrayList;
            Throwable e;
            try {
                JSONObject jSONObject = new JSONObject(str).getJSONObject("data");
                arrayList = new ArrayList(jSONObject.getInt("total"));
                try {
                    JSONArray jSONArray = jSONObject.getJSONArray("list");
                    for (int i = 0; i < jSONArray.length(); i++) {
                        JSONObject jSONObject2 = jSONArray.getJSONObject(i);
                        WeightInfo weightInfo = new WeightInfo();
                        weightInfo.uid = jSONObject2.getInt("fuid");
                        weightInfo.weight = Float.valueOf(jSONObject2.getString("wt")).floatValue();
                        weightInfo.timestamp = jSONObject2.getLong("ts") * 1000;
                        arrayList.add(weightInfo);
                    }
                } catch (JSONException e2) {
                    e = e2;
                    e.printStackTrace();
                    exception(new C1312a(6, e));
                    return arrayList;
                }
            } catch (Throwable e3) {
                Throwable th = e3;
                arrayList = null;
                e = th;
                e.printStackTrace();
                exception(new C1312a(6, e));
                return arrayList;
            }
            return arrayList;
        }
    }

    public void work() {
        GuliLog.tracePrintInfo(tag(), "Sync Start!!");
        if (this.f11285c.isValid()) {
            try {
                String a = URL.m10531a(ClientConstant.aa);
                C1304h c1305d = new ResponseHandler(this.f11284b);
                C1309e c2003k = new C2003k();
                C1313a a2 = new C1313a(this.mWorkParams).m11520a((SyncerUpload) this, new C1999g(this)).m11518a((SyncerUpload) this, new C1301a(a)).m11519a((SyncerUpload) this, c1305d);
                if (c1305d.isSuccessed()) {
                    a2.m11516a((SyncerUpload) this, new C2002j()).m11517a((SyncerUpload) this, c2003k).m11514a(new C2000h(this));
                }
            } catch (Throwable e) {
                GuliLog.m12353a(tag(), "Sync Exception!!", e);
            }
            logState(tag());
            GuliLog.tracePrintInfo(tag(), "Sync Finish!!");
            return;
        }
        GuliLog.tracePrintInfo(tag(), "Sync Stop!!");
    }
}
